package com.ssb.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ssb.po.FileUpVo;
import com.ssb.po.Goods;
import com.ssb.po.GoodsExample;
import com.ssb.service.GoodsService;


@Controller
public class GoodController {
	@Autowired
	private GoodsService goodsService;
	
	/**
	 * 上传实物商品
	 * 
	 * @author 胡俊 2016年10月1日 下午12:13:49
	 */
	 @RequestMapping(value = "/upGoods")  
	    public String upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request,Goods goods) {  
	  
	        System.out.println("开始");  
	        String path = request.getSession().getServletContext().getRealPath("upload");  
	        String fileName = file.getOriginalFilename();  
//	        String fileName = new Date().getTime()+".jpg";  
	        System.out.println(path);  
	        File targetFile = new File(path, fileName);  
	        if(!targetFile.exists()){  
	            targetFile.mkdirs();  
	        }  
	  
	        //保存  
	        try {  
	            file.transferTo(targetFile);  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	    	// 把url放数据库
			goods.setImage(request.getContextPath()+"/upload/"+fileName);
			goods.setStatus(0);
			goodsService.insert(goods);
//	        model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+fileName);  
	        return "wxht/up";  
	    }  
	
	/**
	 * 在售 商品
	 * @return
	 */
	@RequestMapping("/zaiShou")
	public String AllZai(Model m){
		GoodsExample example=new GoodsExample();
		example.createCriteria().andStatusEqualTo(0);
		List<Goods> goods=goodsService.selectByExample(example);
		m.addAttribute("goods",goods);
		return "wxht/zaishou";
	}
	
	/**
	 * 下架 
	 * @return
	 */
	@RequestMapping("/xiajia")
	public String xiajia(Goods goods,Model m){
		goods.setStatus(1);
		goodsService.updateByPrimaryKeySelective(goods);
		GoodsExample example=new GoodsExample();
		example.createCriteria().andStatusEqualTo(0);
		List<Goods> goods1=goodsService.selectByExample(example);
		m.addAttribute("goods",goods1);
		return "wxht/zaishou";
	}

}
